﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;

/// <summary>
/// Summary description for clsCauHoi
/// </summary>
/// 

public class clsCauHoi:BLL
{
	DataClassesDataContext db = new DataClassesDataContext();
	public clsCauHoi()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    private string MaCH;

    public string MaCH1
    {
        get { return MaCH; }
        set { MaCH = value; }
    }
    private string NoidungCH;

    public string NoidungCH1
    {
        get { return NoidungCH; }
        set { NoidungCH = value; }
    }
    private string Da1;

    public string Da11
    {
        get { return Da1; }
        set { Da1 = value; }
    }
    private string Da2;

    public string Da21
    {
        get { return Da2; }
        set { Da2 = value; }
    }
    private string Da3;

    public string Da31
    {
        get { return Da3; }
        set { Da3 = value; }
    }
    private string Da4;

    public string Da41
    {
        get { return Da4; }
        set { Da4 = value; }
    }
    private string DaDung;

    public string DaDung1
    {
        get { return DaDung; }
        set { DaDung = value; }
    }
    private int Mucdo;

    public int Mucdo1
    {
        get { return Mucdo; }
        set { Mucdo = value; }
    }
    
    //[DataObjectMethod(DataObjectMethodType.Select, true)]
    //public List<CauHoi> laych()
    //{
    //    List<CauHoi> l = new List<CauHoi>();
    //    CauHoi ch = new CauHoi();

    //    ch.MaCH1 = "asd";
    //    l.Add(ch);
    //    return l;
    //}
    




    public void ThemCHmoi( string noidungch, string da1, string da2, string da3, string da4, string daDung, int dokho,string machuong)
    {

        tbl_NGANHANGCAUHOI clsCH = new tbl_NGANHANGCAUHOI();

        int idmaxs = (int)db.sp_GetCurrentIndNew("tbl_NGANHANGCAUHOI").ReturnValue;

        string mach = TaoMaAuto("MaCH", "0000", idmaxs);

        clsCH.MaCH = mach;
        clsCH.NoiDungCH = noidungch;
        clsCH.DA1 = da1;
        clsCH.DA2 = da2;
        clsCH.DA3 = da3;
        clsCH.DA4 = da4;
        clsCH.DADUNG = daDung;
        clsCH.Mucdo = dokho;
        clsCH.MaChuong = machuong;
        db.tbl_NGANHANGCAUHOIs.InsertOnSubmit(clsCH);
        db.SubmitChanges();

    }
    public void XoaCH(string mach,string date,string nguoixoa)
    {
        var CHxoa = (from p in db.tbl_NGANHANGCAUHOIs
                    where p.MaCH == mach
                    select p).Single();
        DateTime datenow = Convert.ToDateTime(date);
        CHxoa.Deleted = true;
        CHxoa.Ngayxoa = datenow;
        CHxoa.Nguoixoa = nguoixoa;
        db.SubmitChanges();
    }

    public List<clsCauHoi> dsCHthaythe(string mach,string madt)
    {
        string chuonghoc = (from c in db.tbl_NGANHANGCAUHOIs
                            where c.MaCH == mach
                            select c.MaChuong).Single();
        int mucdo =(from c in db.tbl_NGANHANGCAUHOIs
                       where  c.MaCH==mach
                       select c.Mucdo).Single();
        
        var dsthaythe = (from p in db.tbl_NGANHANGCAUHOIs
                         
                        where p.MaChuong == chuonghoc && p.Mucdo == mucdo && !(from o in db.tbl_CHITIETDETHIs
                                                                                   where o.MaDT==madt
                                                                                   select o.MaCH).Contains(p.MaCH)
                        select new clsCauHoi{
                            MaCH=p.MaCH,
                            NoidungCH=p.NoiDungCH,
                            Da1=p.DA1,
                            Da2 = p.DA2,
                            Da3 = p.DA3,
                            Da4 = p.DA4,
                            DaDung=p.DADUNG

                        } ).ToList();
        return dsthaythe;

    }
    public clsCauHoi LayCHtheoMa(string mach)
    {
        var chsua = (from p in db.tbl_NGANHANGCAUHOIs
                     
                     where p.MaCH == mach
                     select new clsCauHoi
                     {
                         MaCH = p.MaCH,
                         NoidungCH = p.NoiDungCH,
                         Da1 = p.DA1,
                         Da2 = p.DA2,
                         Da3 = p.DA3,
                         Da4 = p.DA4,
                         DaDung = p.DADUNG,
                         
                     }).Single();
        return chsua;
    }
    public bool ktCHdaSD(string mach)
    {
        bool chkt = db.tbl_CHITIETDETHIs.Contains(db.tbl_CHITIETDETHIs.FirstOrDefault(p => p.MaCH == mach));
        return chkt;
    }
    public bool SuaCH(string mach,string noidung,string a,string b,string c,string d, string dan,int dokho,string machuong)
    {
        if (ktCHdaSD(mach))
        {
            return false;
        }
        else
        {
            var suach = (from p in db.tbl_NGANHANGCAUHOIs
                         where p.MaCH == mach
                         select p).FirstOrDefault();
            suach.MaChuong = machuong;
            suach.Mucdo = dokho;
            suach.DA1 = a;
            suach.DA2 = b;
            suach.DA3 = c;
            suach.DA4 = d;
            suach.DADUNG = dan;
            suach.NoiDungCH = noidung;
            db.SubmitChanges();
            return true;
        }
    }
    //public void SuaCH(string noidungch, string da1, string da2, string da3, string da4, string daDung, string dokho)
    //{
    //    //CAUHOI clsCH = new CAUHOI();
    //    var CHsua = (from p in db.tbl_NGANHANGCAUHOIs
    //                 where p.MaCH == 
    //                 select p).Single();
    //    CHsua.NoiDungCH = noidungch;
    //    CHsua.DA1 = da1;
    //    CHsua.DA2 = da2;
    //    CHsua.DA3 = da3;
    //    CHsua.DA4 = da4;
    //    CHsua.DADUNG = daDung;
    //    CHsua.Mucdo = dokho;
    //    db.SubmitChanges();
    //}
}
